<?php

namespace app\admin\model\product;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Log extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'product_buy_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'status_text'
    ];



    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public static function interest($id,$money = 0,$memo)
    {
        Db::startTrans();
        try {
            $buy = \app\common\model\ProductBuy::get($id);

            $request = \think\Request::instance();

            if($buy) {
                $data = [
                    'admin_id' => $buy->admin_id,
                    'user_id' => $buy->user_id,
                    'product_buy_id' => $buy->id,
                    'username' => $buy->username,
                    'price' => $money,
                    'ip' => $request->ip(),
                    'memo' => $memo,
                ];
                self::create($data);
            }

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }


    public function buy()
    {
        return $this->belongsTo('Buy', 'product_buy_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
